/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.guis.resourcePicker.items;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.beans.Resource;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.images.resourcePicker.resourceItem.ResourceItemImageBundle;
import org.tbjd.client.interfaces.Builder;
import org.tbjd.client.interfaces.Decisionmaker;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/**
 * <p>
 * Represents a item displayed in the {@link org.tbjd.client.guis.resourcePicker.ResourcePickerGui ResourcePickerGui}
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class ResourcePickerItem extends Composite implements Builder, Decisionmaker {

	private FocusPanel container = new FocusPanel();
	private Resource resource = null;
	private Widget leftPanel = null;
	private Map<Integer, Object> map = null;

	public ResourcePickerItem() {
	}

	/**
	 * 
	 * @param eachResource the {@link org.tbjd.client.beans.Resource Resource} bean
	 */
	public ResourcePickerItem(Resource eachResource) {
		this.resource = eachResource;
		this.build();
		this.initWidget(this.container);
		this.container.sinkEvents(Event.ONDBLCLICK);
	}

	public void build() {
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStyleName("tbjd-resourcePickerItem_horizontalPanel");

		// left panel
		this.leftPanel = getLeftPanel();
		leftPanel.setStyleName("tbjd-resourcePickerItem_leftPanel");

		// center panel
		VerticalPanel centerPanel = getCenterPanel();
		centerPanel.setHeight(this.container.getOffsetHeight() + "%");

		// right panel
		VerticalPanel rightPanel = getRightPanel();

		horizontalPanel.add(leftPanel);
		horizontalPanel.add(centerPanel);
		horizontalPanel.add(rightPanel);
		horizontalPanel.setCellVerticalAlignment(rightPanel, HasVerticalAlignment.ALIGN_BOTTOM);
		horizontalPanel.setCellHorizontalAlignment(rightPanel, HasHorizontalAlignment.ALIGN_RIGHT);

		this.container.add(horizontalPanel);
		this.container.setStyleName("tbjd-resourcePickerItem");
	}

	/**
	 * Contains the ResourcePickerItem icon
	 * @return com.google.gwt.user.client.ui.Widget
	 */
	private Widget getLeftPanel() {
		ResourceItemImageBundle resourceImageBundle = (ResourceItemImageBundle) GWT.create(ResourceItemImageBundle.class);

		return resourceImageBundle.man_color_13x37().createImage();
	}

	/**
	 * Contains the ResourcePickerItem's infos such as name and initials of the Resource
	 * @return com.google.gwt.user.client.ui.VerticalPanel
	 */
	private VerticalPanel getCenterPanel() {
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setStyleName("tbjd-resourcePickerItem_centerPanel");

		Label name = new Label(resource.getName());
		name.setStyleName("tbjd-resourcePickerItem_name");
		verticalPanel.add(name);
		verticalPanel.setCellHorizontalAlignment(name, HasHorizontalAlignment.ALIGN_LEFT);
		verticalPanel.setCellVerticalAlignment(name, HasVerticalAlignment.ALIGN_TOP);

		Label initials = new Label(resource.getInitials());
		initials.setStyleName("tbjd-resourcePickerItem_intials");
		verticalPanel.add(initials);
		verticalPanel.setCellHorizontalAlignment(initials, HasHorizontalAlignment.ALIGN_LEFT);
		verticalPanel.setCellVerticalAlignment(initials, HasVerticalAlignment.ALIGN_BOTTOM);

		return verticalPanel;
	}

	/**
	 * Contains the ratio of the Resource
	 * @return com.google.gwt.user.client.ui.VerticalPanel
	 */
	private VerticalPanel getRightPanel() {
		VerticalPanel panel = new VerticalPanel();
		panel.setStyleName("tbjd-resourcePickerItem_rightPanel");

		Label ratio = new Label(resource.getRatio() + "%");
		ratio.setStyleName("tbjd-resourcePickerItem_ratio");

		panel.add(ratio);
		panel.setCellHorizontalAlignment(ratio, HasHorizontalAlignment.ALIGN_RIGHT);

		return panel;
	}

	public Widget getHandle() {
		return this.leftPanel;
	}

	public Widget getBuildedWidget() {
		return this.container;
	}

	public void onBrowserEvent(Event event) {
		super.onBrowserEvent(event);
		decide(event);
	}

	public void decide(Object action) {
		if (action instanceof Event && DOM.eventGetType((Event) action) == Event.ONDBLCLICK) {
			System.out.println("ResourcePickerItem.decide() ONDBLCLICK "+this.resource.getName());
			this.map = new HashMap<Integer, Object>();
			this.map.put(NotificationsConstants.OPEN_RESOURCE, this.resource.getId());

			try {
				delegate(this.map);
			} catch (NoUpperDecisionmakerFoundException e) {
				e.printStackTrace();
			} catch (NoUpperMediatorFoundException e) {
				e.printStackTrace();
			}
		}
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		System.out.println("ResourcePickerItem.delegate");
		// passes info to the upper decisionmaker
		Widget parent = ((Widget) this).getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("no parent or responsible founded !");
		}
	}

	public void handle(Object action) {
	}

	public Resource getResource() {
		return resource;
	}

}
